home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / apt-xapian-index / examples / axi-query.py < prev    next >
Encoding:
Python Source  |  2008-04-23  |  3.4 KB  |  107 lines

  1. #!/usr/bin/python
  2.  
  3. #
  4. # axi-query - Example program to query the apt-xapian-index
  5. #
  6. # Copyright (C) 2007  Enrico Zini <enrico@debian.org>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21. #
  22.  
  23. from aptxapianindex import *
  24. from optparse import OptionParser
  25. import sys
  26.  
  27. VERSION="0.1"
  28.  
  29. class Parser(OptionParser):
  30.     def __init__(self, *args, **kwargs):
  31.         OptionParser.__init__(self, *args, **kwargs)
  32.  
  33.     def error(self, msg):
  34.         sys.stderr.write("%s: error: %s\n\n" % (self.get_prog_name(), msg))
  35.         self.print_help(sys.stderr)
  36.         sys.exit(2)
  37.  
  38. parser = Parser(usage="usage: %prog [options]",
  39.                 version="%prog "+ VERSION,
  40.                 description="Query the Apt Xapian index.  Command line arguments can be keywords or Debtags tags")
  41. parser.add_option("-s", "--sort", help="sort by the given value, as listed in %s" % XAPIANDBVALUES)
  42.  
  43. (options, args) = parser.parse_args()
  44.  
  45.  
  46. import os
  47. import xapian
  48. import warnings
  49. # Yes, apt, thanks, I know, the api isn't stable, thank you so very much
  50. #warnings.simplefilter('ignore', FutureWarning)
  51. warnings.filterwarnings("ignore","apt API not stable yet")
  52. import apt
  53. warnings.resetwarnings()
  54.  
  55. # Access the Xapian index
  56. db = xapian.Database(XAPIANDB)
  57.  
  58. # Build the query
  59. stemmer = xapian.Stem("english")
  60. terms = []
  61. for word in args:
  62.     if word.islower() and word.find("::") != -1:
  63.         # If it's lowercase and contains, :: it's a tag
  64.         # TODO: lookup in debtags' vocabulary instead
  65.         terms.append("XT"+word)
  66.     else:
  67.         # Else we make a term
  68.         word = word.lower()
  69.         terms.append(word)
  70.         stem = stemmer(word)
  71.         # If it has stemming, add that to the query, too
  72.         if stem != word:
  73.             terms.append("Z"+stem)
  74. query = xapian.Query(xapian.Query.OP_OR, terms)
  75.  
  76. # Perform the query
  77. enquire = xapian.Enquire(db)
  78. enquire.set_query(query)
  79. if options.sort:
  80.     values = readValueDB(XAPIANDBVALUES)
  81.  
  82.     # If we don't sort by relevance, we need to specify a cutoff in order to
  83.     # remove poor results from the output
  84.     #
  85.     # Note: ept-cache implements an adaptive cutoff as follows:
  86.     # 1. Retrieve only one result, with default sorting.  Read its relevance as
  87.     #    the maximum relevance.
  88.     # 2. Set the cutoff as some percentage of the maximum relevance
  89.     # 3. Set sort by the wanted value
  90.     # 4. Perform the query
  91.     enquire.set_cutoff(60)
  92.  
  93.     # Sort by the requested value
  94.     enquire.set_sort_by_value(values[options.sort])
  95.  
  96. # Display the results.
  97. cache = apt.Cache()
  98. matches = enquire.get_mset(0, 20)
  99. print "%i results found." % matches.get_matches_estimated()
  100. print "Results 1-%i:" % matches.size()
  101. for m in matches:
  102.     name = m[xapian.MSET_DOCUMENT].get_data()
  103.     pkg = cache[name]
  104.     print "%i%% %s - %s" % (m[xapian.MSET_PERCENT], name, pkg.summary)
  105.  
  106. sys.exit(0)
  107.